package com.polaris.live.ui.live.proxy

import android.content.Context
import com.lxj.xpopup.XPopup
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.dialog.WarmingTimeDialog
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.im.sfs.LIVE_WARN_MESSAGE
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.im.sfs.bean.live.payload.LiveWarningPayloadBean
import com.polaris.live.popup.FeedBackPopup

/**
 * LivePublicProxy
 *
 * @author Created by lucas on 2023/11/24 13:48
 */
class LivePublicProxy(
    private val context: Context,
    private val viewModel: BaseViewModel
) {

    fun onPublicMessage(liveMessage: SendLiveMessage?) {
        when (liveMessage?.type) {
            //这里是警告消息 警告消息也不需要到对应的页面做单独处理  所以直接展示就行
            LIVE_WARN_MESSAGE -> {
                val payload = liveMessage.payload as LiveWarningPayloadBean
                if (payload.duration == null) {
                    warningDialog(liveMessage, payload)
                } else {
                    timeWarningDialog(liveMessage, payload)
                }
            }

        }
    }

    private fun warningDialog(
        liveMessage: SendLiveMessage,
        payload: LiveWarningPayloadBean,
    ) {
        XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .asCustom(
                ConfirmCanalDetailDialog(
                    context,
                    payload.title,
                    liveMessage.content,
                    payload.mastButton,
                    payload.feedbackButton,
                    payload.highlights,
                    {}, {
                        XPopup.Builder(context)
                            .hasShadowBg(false)
                            .isDestroyOnDismiss(true)
                            .asCustom(FeedBackPopup(context, viewModel))
                            .show()
                    }
                )
            ).show()
    }

    private fun timeWarningDialog(
        liveMessage: SendLiveMessage,
        payload: LiveWarningPayloadBean,
    ) {
        XPopup.Builder(context)
            .hasShadowBg(false)
            .isDestroyOnDismiss(true)
            .asCustom(
                WarmingTimeDialog(
                    context,
                    payload.title,
                    liveMessage.content,
                    payload.duration ?: 0,
                    payload.mastButton,
                    viewModel,
                    payload.highlights,
                )
            ).show()
    }
}